package org.example.com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 解决跨域问题
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration conf = new CorsConfiguration();
        conf.addAllowedOriginPattern("*");   // 允许所有域名，生产可改具体域名
        conf.addAllowedHeader("*");          // 允许任何头
        conf.addAllowedMethod("*");          // GET/POST/PUT/DELETE...
        conf.setAllowCredentials(true);      // 允许带 cookie

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", conf); // 对所有接口生效
        return new CorsFilter(source);
    }
}